package com.whjz.dcfs.jzxx.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.whjz.dcfs.common.response.JsonMsgResponse;
import com.whjz.dcfs.jzxx.struts.service.JZSBService;
import com.whjz.dcfs.pub.excel.CollectionUtils;
import com.whjz.dcfs.pub.excel.ExcelData;
import com.whjz.dcfs.pub.excel.ExcelUtils;
import com.whjz.dcfs.pub.excel.entity.ProductEditExcelVO;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.whjz.dcfs.jzxx.dao.IDcfsJzSbJbxxDAO;
import com.whjz.dcfs.jzxx.model.DcfsJzSbJbxx;
import com.whjz.dcfs.util.BaseAction;
import com.whjz.dcfs.util.UserUtil;

public class JZSBAction extends BaseAction{

	private Logger logger = LoggerFactory.getLogger(JZSBAction.class);

	private static final long serialVersionUID = 1L;
	@Autowired
	public IDcfsJzSbJbxxDAO dcfsJzSbJbxxDAO;
	
	private DcfsJzSbJbxx dcfsJzSbJbxx;

	private File uploadExcelFile;

	@Autowired
	private JZSBService jzsbService;

	public void insertDcfsJzSbJbxxByExcel() throws Exception{
		boolean bizStatus = true;
		String msg = "成功添加";
		ExcelData<ProductEditExcelVO> excelData = null;
		try {
			excelData = ExcelUtils.read(new FileInputStream(uploadExcelFile), ProductEditExcelVO.class, 1);
			if (excelData!=null && !CollectionUtils.isNullOrEmpty(excelData.getResultList())) {
				int count = jzsbService.insertDcfsJzSbJbxxByExcel(excelData.getResultList());
				msg += count+"条数据";
			} else {
				bizStatus = false;
				msg = "表格数据为空";
			}
		}catch (Exception e){
			bizStatus = false;
			msg = e.getMessage();
		}
		JsonMsgResponse jsonMsgResponse = new JsonMsgResponse();
		jsonMsgResponse.setSuccess(true);
		jsonMsgResponse.setBizStatus(bizStatus);
		jsonMsgResponse.setMsg(msg);
		getHttpServletResponse().setCharacterEncoding("utf-8");
		getHttpServletResponse().setContentType("text/html");
		getHttpServletResponse().getWriter().println(JSON.toJSONString(jsonMsgResponse));
	}

	public void insertDcfsJzSbJbxx() throws Exception{
		try{
			//getHttpServletRequest().setCharacterEncoding("utf-8");
			//getHttpServletResponse().setCharacterEncoding("utf-8");
			dcfsJzSbJbxx.setJzflag("0");
			dcfsJzSbJbxxDAO.insertDcfsJzSbJbxx(dcfsJzSbJbxx);
			getHttpServletResponse().getWriter().println("{success:'true',msg:'添加成功'}");
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void  getDcfsJzSbJbxxByPK() throws Exception{
		DcfsJzSbJbxx jbxx=dcfsJzSbJbxxDAO.getDcfsJzSbJbxxByPK(dcfsJzSbJbxx);
		
	}
	
	public void updateDcfsJzSbJbxxByCond() throws Exception{
		try{
			dcfsJzSbJbxxDAO.updateDcfsJzSbJbxxByCond(dcfsJzSbJbxx);
			getHttpServletResponse().getWriter().println("{success:'true',msg:'修改成功'}");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void deleteDcfsJzSbJbxxByCond() throws Exception{
		try{
			dcfsJzSbJbxx.setJzflag("1");
			dcfsJzSbJbxxDAO.updateDcfsJzSbJbxxByCond(dcfsJzSbJbxx);
			getHttpServletResponse().getWriter().println("{success:'true',msg:'删除成功'}");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void getDcfsJzSbJbxxListByCond() throws Exception{
		try{
			getHttpServletRequest().setCharacterEncoding("utf-8");
			getHttpServletResponse().setCharacterEncoding("utf-8");
			String fsybh=getHttpServletRequest().getParameter("dcfsJzSbJbxx.jzsbbh");
			String start=getHttpServletRequest().getParameter("start");
			String typeCode =getHttpServletRequest().getParameter("typeCode");
			String limit=getHttpServletRequest().getParameter("limit");
			String xzqh=UserUtil.findUserFromSession(getSession().get("user"),"getConcern");
			if(xzqh.indexOf("区")!=-1){
				if(dcfsJzSbJbxx==null){
					dcfsJzSbJbxx=new DcfsJzSbJbxx();
				}
				dcfsJzSbJbxx.setJzsbqy(xzqh);
			}
			List<DcfsJzSbJbxx> list=dcfsJzSbJbxxDAO.getDcfsJzSbJbxxListByCond(dcfsJzSbJbxx,fsybh,start,limit,typeCode);
			int count=dcfsJzSbJbxxDAO.getDcfsJzSbJbxxListCount(dcfsJzSbJbxx, fsybh, typeCode);
//			System.out.println("---------------count"+count);
			StringBuffer sb=new StringBuffer();
			sb.append("{root:[");
			//System.out.println(list.size());
			if(list!=null&&list.size()>0){
				for(int i=0;i<list.size();i++){
					sb.append("{");
					sb.append("JZ_SB_BH:'"+(list.get(i).getJzsbbh()==null?"":list.get(i).getJzsbbh())+"',");
					sb.append("JZ_SB_MC:'"+(list.get(i).getJzsbmc()==null?"":list.get(i).getJzsbmc())+"',");
					sb.append("JZ_SB_AZWZ:'"+(list.get(i).getJzsbazwz()==null?"":list.get(i).getJzsbazwz())+"',");
					sb.append("JZ_FSTX_JSFS:'"+(list.get(i).getJzfstxjsfs()==null?"":list.get(i).getJzfstxjsfs())+"',");
					sb.append("JZ_FSTX_JHFS:'"+(list.get(i).getJzfstxjhfs()==null?"":list.get(i).getJzfstxjhfs())+"',");
					sb.append("JZ_SB_XH:'"+(list.get(i).getJzsbxh()==null?"":list.get(i).getJzsbxh())+"',");
					sb.append("JZ_SB_WLLX:'"+(list.get(i).getJzsbwllx()==null?"":list.get(i).getJzsbwllx())+"',");
					sb.append("YT_MC:'"+(list.get(i).getYtmc()==null?"":list.get(i).getYtmc())+"',");
					sb.append("YT_BH:'"+(list.get(i).getYtbh()==null?"":list.get(i).getYtbh())+"',");
					sb.append("JZ_SB_QYSJ:'"+(list.get(i).getJzsbqysj()==null?"":list.get(i).getJzsbqysj())+"',");
					sb.append("JZ_BCGL:'"+(list.get(i).getJzbcgl()==null?"":list.get(i).getJzbcgl())+"',");
					sb.append("JZ_ZPPZ:'"+(list.get(i).getJzzppz()==null?"":list.get(i).getJzzppz())+"',");
					sb.append("JZ_XXPD_1:'"+(list.get(i).getJzxxpd1()==null?"":list.get(i).getJzxxpd1())+"',");
					sb.append("JZ_XXPD_2:'"+(list.get(i).getJzxxpd2()==null?"":list.get(i).getJzxxpd2())+"',");
					sb.append("JZ_TXZY:'"+(list.get(i).getJztxzy()==null?"":list.get(i).getJztxzy())+"',");
					sb.append("JZ_FSTX_LX:'"+(list.get(i).getJzfstxlx()==null?"":list.get(i).getJzfstxlx())+"',");
					sb.append("JZ_FSTX_FWJ:'"+(list.get(i).getJzfstxfwj()==null?"":list.get(i).getJzfstxfwj())+"',");
					sb.append("JZ_FSTX_XPJ:'"+(list.get(i).getJzfstxxpj()==null?"":list.get(i).getJzfstxxpj())+"',");
					sb.append("JZ_FSTX_JD:'"+(list.get(i).getJzfstxjd()==null?"":list.get(i).getJzfstxjd())+"',");
					sb.append("JZ_FSTX_WD:'"+(list.get(i).getJzfstxwd()==null?"":list.get(i).getJzfstxwd())+"',");
					sb.append("JZ_FSTX_JSGD:'"+(list.get(i).getJzfstxjsgd()==null?"":list.get(i).getJzfstxjsgd())+"',");
					sb.append("JZ_FSTX_SL:'"+(list.get(i).getJzfstxsl()==null?"":list.get(i).getJzfstxsl())+"',");
					sb.append("JZ_BH:'"+(list.get(i).getJzbh()==null?"":list.get(i).getJzbh())+"',");
					sb.append("JZ_MC:'"+(list.get(i).getJzmc()==null?"":list.get(i).getJzmc())+"',");
					sb.append("JZ_DD:'"+(list.get(i).getJzdd()==null?"":list.get(i).getJzdd())+"',");
					sb.append("BD_JD:'"+(list.get(i).getBdjd()==null?"":list.get(i).getBdjd())+"',");
					sb.append("BD_WD:'"+(list.get(i).getBdwd()==null?"":list.get(i).getBdwd())+"',");
					sb.append("JZ_SB_QY:'"+(list.get(i).getJzsbqy()==null?"":list.get(i).getJzsbqy())+"',");
					sb.append("JZ_BZ:'"+(list.get(i).getJzbz()==null?"":list.get(i).getJzbz())+"',");
					sb.append("JZ_YYS_XX:'"+(list.get(i).getJzyysxx()==null?"":list.get(i).getJzyysxx())+"',");
					sb.append("JZ_HP_ZT:'"+(list.get(i).getJzhpzt()==null?"":list.get(i).getJzhpzt())+"',");
					sb.append("JZ_JC_DW:'"+(list.get(i).getJzjcdw()==null?"":list.get(i).getJzjcdw())+"',");
					sb.append("JZ_YS_DW:'"+(list.get(i).getJzysdw()==null?"":list.get(i).getJzysdw())+"'");
					sb.append("}");
					if(i < list.size()-1){
						sb.append(",");
					}
					
				}
				
			}
			sb.append("],totalCount:'"+count+"'");
			sb.append("}");
//			System.out.println(sb);
			getHttpServletResponse().getWriter().print(sb);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public DcfsJzSbJbxx getDcfsJzSbJbxx() {
		return dcfsJzSbJbxx;
	}

	public void setDcfsJzSbJbxx(DcfsJzSbJbxx dcfsJzSbJbxx) {
		this.dcfsJzSbJbxx = dcfsJzSbJbxx;
	}


	public File getUploadExcelFile() {
		return uploadExcelFile;
	}

	public void setUploadExcelFile(File uploadExcelFile) {
		this.uploadExcelFile = uploadExcelFile;
	}
}
